package tests;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.Date;

import modelLayer.Customer;
import modelLayer.Invoice;

import org.junit.Test;

import dbLayer.DbInvoice;
import dbLayer.DbPartner;

public class TestDbInvoice {
	
	@Test
	public void insertInvoiceTest() {
		DbInvoice dbinv = new DbInvoice();
		DbPartner dbpar = new DbPartner();
		Invoice inv = new Invoice();
		Customer c = new Customer();
		try{
			c = dbpar.retrieveCustomerFromDb(2);
			System.out.println(c.getName());
		}catch(Exception e){
			System.out.println("Cant get a customer to insert.");
		}
		
		inv.setPaymentDate("2014-22-10");
		inv.setCustomer(c);
		
		try{
			System.out.println(inv.getCustomer().getName());
			dbinv.insertInvoice(inv);
		}catch(Exception e){
			fail("Cant insert invoice here.");
		}
	}
	
	
	
	@Test
	public void retrieveInvoiceFromDbTest(){
		DbInvoice dbinv = new DbInvoice();
		Invoice inv = new Invoice();
		try{
			inv = dbinv.retrieveInvoiceByInvoiceNo(2);
		}catch(Exception e){
			System.out.println("Cant get any invoices by customerid.");
		}
		if(inv != null)
			System.out.println("Payment date: " + inv.getPaymentDate());
		else
			System.out.println("Invoice is null.");
	}
	
	@Test
	public void retrieveAllInvoicesFromDbTest(){
		DbInvoice dbinv = new DbInvoice();
		ArrayList<Invoice> invList = new ArrayList<Invoice>();
		try{
			invList = dbinv.retrieveAllInvoices();
		}catch(Exception e){
			fail("Cant get list of invoices.");
		}
	}
	
	@Test
	public void retrieveInvoiceByCustomerId(){
		DbInvoice dbinv = new DbInvoice();
		Invoice inv = new Invoice();
		
		try{
			inv = dbinv.retrieveInvoiceFromDb(2);
			System.out.println("Date: " + inv.getPaymentDate() + ", InvoiceNo: " + inv.getInvoiceNo() + ", CustomerId: " + inv.getCustomer().getPartnerId());
		}catch(Exception e){
			fail("Cant get invoice by customerId");
		}
	}
	
	
}
